# Copyright (c) 2006 Charl P. Botha <http://cpbotha.net/>
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Script for building a complete release.  Uses build_package.py.
"""

import os
import shutil
import sys

MAKENSIS = '"c:\\Program Files\\NSIS\\makensis.exe" envedit.nsi'

def get_our_dir():
    dirname = os.path.dirname(sys.argv[0])
    if dirname and dirname != os.curdir:
        our_dir = dirname
    else:
        our_dir = os.getcwd()

    return our_dir


def main():
    our_dir = get_our_dir()
    os.chdir(our_dir)

    # build the package
    import build_package
    build_package.main()

    # now run nsis
    os.system('%s' % (MAKENSIS,))

    # now rename the resultant exe file to the current envedit version
    # by inserting it in front, we make sure we get the correct version
    sys.path.insert( 0, os.path.abspath('..') )
    import version

    # create new filename
    setup_name = 'envedit-%s-setup.exe' % (version.VERSION,)
    zip_name = 'envedit-%s.zip' % (version.VERSION,)
    source_name = 'envedit-%s-source.zip' % (version.VERSION,)

    # first the setup ##################################################
    if os.path.exists(setup_name):
        print "Nuking %s..." % (setup_name,)
        os.remove(setup_name)

    os.rename('enveditsetup.exe', setup_name)
    print "Created %s." % (setup_name,)

    # then the binary ZIP ##############################################
    if os.path.exists('envedit'):
        print "Nuking directory envedit..."
        shutil.rmtree('envedit')

    os.rename('distenvedit', 'envedit')

    if os.path.exists(zip_name):
        print "Nuking %s..." % (zip_name,)
    
    os.system('zip -qrp %s envedit' % (zip_name,))
    print "Created %s." % (zip_name,)

    os.rename('envedit', 'distenvedit')

    # finally the source ###############################################
    print "Not doing source yet..."


if __name__ == '__main__':
    main()
